using System;
using System.Text;
using System.Collections;
using System.DirectoryServices;

namespace BEGDC.CC.BL.Workflow
{
	/// <summary>
	/// Summary description for LdapAuthentication.
	/// </summary>
	public class LdapAuthentication
	{

		private string _path;
//		private string  _filterAttribute ;
		private string _type;

		/// <summary>
		/// 
		/// </summary>
		/// <param name="path">LDAP path</param>
		public LdapAuthentication(string path,string type)
		{
			_path = path;
			_type = type;
		}

		public bool IsAuthenticated(string domain, string username, string pwd)
		{
			//string domainAndUsername = domain + @"\" + username;
			DirectoryEntry entry = null;

			try
			{ 
				entry = new DirectoryEntry( _path, username, pwd);

				switch(_type)
				{
					case "SSL":
						entry.AuthenticationType = AuthenticationTypes.SecureSocketsLayer;
						break;
					case "ServerBind":
						entry.AuthenticationType = AuthenticationTypes.ServerBind;
						break;
					case "None":
						entry.AuthenticationType = AuthenticationTypes.None;
						break;
					default:
						entry.AuthenticationType = AuthenticationTypes.None;
						break;
				};

				//Bind to the native AdsObject to force authentication.
				Object obj = entry.NativeObject;
				DirectorySearcher search = new DirectorySearcher(entry);
				
				search.Filter = "(cn=" + username + ")";
				search.SearchScope = SearchScope.Subtree; 
				search.PropertyNamesOnly = true; 					

				search.PropertiesToLoad.Add("cn");
				SearchResult result = search.FindOne();
				if(null == result)
				{
					return false;
				}
				//Update the new path to the user in the directory.

//				_path = result.Path;
//				_filterAttribute = (String)result.Properties["cn"][0];
			}
			catch (Exception ex)
			{
				throw new Exception("Error authenticating user. " + ex.Message);
			}
			finally
			{
				if (entry != null)
					entry.Close();
			}
			return true;
		}

//		public string GetGroups()
//		{
//			DirectorySearcher search = new DirectorySearcher(_path);
//			search.Filter = "(cn=" + _filterAttribute + ")";
//			search.PropertiesToLoad.Add("memberOf");
//			StringBuilder groupNames = new StringBuilder();
//			try
//			{
//				SearchResult result = search.FindOne();
//				int propertyCount = result.Properties["memberOf"].Count;
//				String dn;
//				int equalsIndex, commaIndex;
//
//				for( int propertyCounter = 0; propertyCounter < propertyCount;
//					propertyCounter++)
//				{
//					dn = (String)result.Properties["memberOf"][propertyCounter];
//
//					equalsIndex = dn.IndexOf("=", 1);
//					commaIndex = dn.IndexOf(",", 1);
//					if (-1 == equalsIndex)
//					{
//						return null;
//					}
//					groupNames.Append(dn.Substring((equalsIndex + 1), 
//						(commaIndex - equalsIndex) - 1));
//					groupNames.Append("|");
//				}
//			}
//			catch(Exception ex)
//			{
//				throw new Exception("Error obtaining group names." + ex.Message);
//			} 
//			return groupNames.ToString();
//		}

	}
}
